package Array;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname 两数之和1
 * @Description TODO
 * @Date 2022/1/9 22:13
 * @Created by xjl
 */
public class 两个数之和1 {
    /**
     * @description 采用的是Hashmap的方式来实现相关   key:target-num[i] ,value: 位置下标
      * @param: nums
     * @param: target
     * @date: 2022/12/5 14:05
     * @return: int[]
     * @author: xjl
    */
    public int[] twoSum(int[] nums, int target) {
        if (nums.length == 0) {
            return new int[]{};
        }
        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
            if (hashtable.containsKey(target - nums[i])) {
                return new int[]{hashtable.get(target - nums[i]), i};
            }
            hashtable.put(nums[i], i);
        }
        return new int[0];
    }

    @Test
    public void test() {
        int[] array = {2, 7, 11, 15};
        int target = 9;
        int[] res = twoSum(array, target);
        System.out.println(res[0] + "==" + res[1]);
    }
}
